//
//  IMVibrateModels.m
//  DualMediationSDK
//
//  Created by YeJary on 2025/1/27.
//

#import "IMVibrateModels.h"

@implementation IMVibrateConfig

- (instancetype)init {
    self = [super init];
    if (self) {
        _type = IMVibrateTypeLight;
        _intensity = IMVibrateIntensityMedium;
        _mode = IMVibrateModeOnce;
        _duration = 0;
        _repeatCount = -1;
    }
    return self;
}

+ (instancetype)configWithDuration:(NSTimeInterval)duration {
    IMVibrateConfig *config = [[IMVibrateConfig alloc] init];
    config.duration = duration;
    config.mode = IMVibrateModeOnce;
    return config;
}

+ (instancetype)configWithPattern:(NSArray<NSNumber *> *)pattern
                      repeatCount:(NSInteger)repeatCount {
    IMVibrateConfig *config = [[IMVibrateConfig alloc] init];
    config.pattern = pattern;
    config.repeatCount = repeatCount;
    config.mode = IMVibrateModePattern;
    return config;
}

+ (instancetype)configWithType:(IMVibrateType)type
                     intensity:(IMVibrateIntensity)intensity {
    IMVibrateConfig *config = [[IMVibrateConfig alloc] init];
    config.type = type;
    config.intensity = intensity;
    config.mode = IMVibrateModeOnce;
    return config;
}

- (BOOL)isValid {
    // 检查基本参数
    if (self.mode == IMVibrateModeOnce) {
        if (self.duration <= 0 && self.type == IMVibrateTypeLight) {
            return NO;
        }
    } else if (self.mode == IMVibrateModePattern) {
        if (!self.pattern || self.pattern.count == 0) {
            return NO;
        }
        // 检查模式数组中的值
        for (NSNumber *value in self.pattern) {
            if ([value doubleValue] < 0) {
                return NO;
            }
        }
    }
    
    return YES;
}

- (NSString *)configDescription {
    NSMutableString *desc = [NSMutableString string];
    
    // 震动类型描述
    NSArray *typeNames = @[@"轻微", @"中等", @"强烈", @"选择", @"冲击", @"通知", @"成功", @"警告", @"错误"];
    if (self.type < typeNames.count) {
        [desc appendFormat:@"类型:%@", typeNames[self.type]];
    }
    
    // 震动强度描述
    NSArray *intensityNames = @[@"轻微", @"中等", @"强烈"];
    if (self.intensity < intensityNames.count) {
        [desc appendFormat:@" 强度:%@", intensityNames[self.intensity]];
    }
    
    // 震动模式描述
    if (self.mode == IMVibrateModeOnce) {
        [desc appendFormat:@" 模式:单次"];
        if (self.duration > 0) {
            [desc appendFormat:@" 时长:%.0fms", self.duration];
        }
    } else if (self.mode == IMVibrateModePattern) {
        [desc appendFormat:@" 模式:模式震动"];
        if (self.pattern) {
            [desc appendFormat:@" 频率:%@", self.pattern];
        }
        if (self.repeatCount == 0) {
            [desc appendString:@" 重复:无限"];
        } else if (self.repeatCount > 0) {
            [desc appendFormat:@" 重复:%ld次", (long)self.repeatCount];
        }
    } else if (self.mode == IMVibrateModeRepeat) {
        [desc appendFormat:@" 模式:重复震动"];
        if (self.repeatCount == 0) {
            [desc appendString:@" 重复:无限"];
        } else if (self.repeatCount > 0) {
            [desc appendFormat:@" 重复:%ld次", (long)self.repeatCount];
        }
    }
    
    return [desc copy];
}

- (NSString *)description {
    return [NSString stringWithFormat:@"<%@: %p> %@",
            NSStringFromClass([self class]), self, [self configDescription]];
}

@end

@implementation IMVibrateResult

+ (instancetype)successWithVibrateId:(NSString *)vibrateId {
    IMVibrateResult *result = [[IMVibrateResult alloc] init];
    result.success = YES;
    result.vibrateId = vibrateId;
    result.errorCode = 0;
    return result;
}

+ (instancetype)failureWithCode:(NSInteger)errorCode
                        message:(NSString *)errorMessage {
    IMVibrateResult *result = [[IMVibrateResult alloc] init];
    result.success = NO;
    result.errorCode = errorCode;
    result.errorMessage = errorMessage;
    return result;
}

- (NSString *)description {
    if (self.success) {
        return [NSString stringWithFormat:@"<%@: %p> 成功 vibrateId:%@",
                NSStringFromClass([self class]), self, self.vibrateId];
    } else {
        return [NSString stringWithFormat:@"<%@: %p> 失败 code:%ld message:%@",
                NSStringFromClass([self class]), self, (long)self.errorCode, self.errorMessage];
    }
}

@end